package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.mapper.EmpMapper;
import com.usian.pojo.Emp;
import com.usian.query.EmpDto;
import com.usian.service.EmpService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> findAll() {
        return empMapper.selectList(null);
    }

    @Override
    public Emp findById(int id) {
        return empMapper.selectById(id);
    }

    @Override
    public Emp findByIdAndName(int id, String name) {
        //QueryWrapper<Emp> queryWrapper =new QueryWrapper<>();
        //queryWrapper.lambda().eq(Emp::getEid,id);
        //queryWrapper.lambda().eq(Emp::getEname,name);

        LambdaQueryWrapper<Emp> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Emp::getEid,id);
        lambdaQueryWrapper.eq(Emp::getEname,name);

        return empMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public void saveEmp(Emp emp) {
        empMapper.insert(emp);
    }

    @Override
    public void updateEmp(Emp emp) {
        empMapper.updateById(emp);
    }

    @Override
    public PageResult<Emp> findAll(EmpDto empDto) {
        List<Emp> empList = empMapper.findAll(empDto);
        int count = empMapper.findCount(empDto);

        PageResult<Emp> result =new PageResult<>();
        result.setRows(empList);
        result.setTotal(Long.valueOf(String.valueOf(count)));
        return result;
    }

    @Override
    public void deleteById(Integer id) {
        empMapper.deleteById(id);
    }

}
